package com.jd.seg;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.CharUtils;
import org.apache.commons.lang.StringUtils;

import com.jd.seg.util.Util;

public class RMMSeg implements ISegmenter {

	@Override
	public SegResult seg(String str) {
		SegResult segResult = null;
		if(StringUtils.isNotEmpty(str)){
			char [] chars = str.toCharArray();
			int end = chars.length;
			List<String> words = new ArrayList<String>();
			segResult = new SegResult();
			List<Word> wordList = new ArrayList<Word>();
			List<Word> unLogingWordList = new ArrayList<Word>();
			boolean isEnd = false;
			while(!isEnd){
				int maxLen = DictUtil.getMaxLenEndBy(new String(ArrayUtils.subarray(chars, end-1, end)));
				if(maxLen==0){
					maxLen = 1;
				}
				int start = end-maxLen;
				if(start<0){
					start = 0;
				}
				for(;start<end;start++){
					char[] tmpChar = ArrayUtils.subarray(chars, start, end);
					String word = Util.toString(tmpChar);
					if(DictUtil.match(word)){
						//如果word in 单位词典，那么计算前面是否是数字。
						if(DictUtil.inUnitsDic(word)){
							boolean isNumber = true;
							int posi = start;
							while(isNumber){
								posi -= 1;
								if(posi>=0){
									if(!CharUtils.isAsciiNumeric(chars[posi])){
										isNumber = false;
										posi += 1;
									}
								}else{
									posi += 1;
									isNumber = false;
								}
							}
							if(posi < start){
								start = posi;
								word = Util.toString(ArrayUtils.subarray(chars, start, end));
							}
						}
						Word mWords = new Word();
						mWords.setContent(word);
						mWords.setDomain(null);
						mWords.setFrequency(0);
						mWords.setSort("");
						wordList.add(mWords);
						end = start;
						if(end==0){
							isEnd = true;
						}
						break;
					}else{
						if(tmpChar.length == 1){
								if(CharUtils.isAsciiAlphanumeric(chars[start])){
								int posi = start;
								boolean isAlpha = true;
								while(isAlpha){
									posi -= 1;
									if(posi>=0){
										if(!CharUtils.isAsciiAlphanumeric(chars[posi])){
											isAlpha = false;
											posi += 1;
											end = posi;
										}
									}else{
										posi += 1;
										isAlpha = false;
										end = posi;
									}
								}
								word = Util.toString(ArrayUtils.subarray(chars, posi, start+1));
								Word mWords = new Word();
								mWords.setContent(word);
								mWords.setDomain(null);
								mWords.setFrequency(0);
								mWords.setSort("");
								if(!DictUtil.match(word)){
									unLogingWordList.add(mWords);
								}
								wordList.add(mWords);
							}
							else{
								Word mWords = new Word();
								mWords.setContent(word);
								mWords.setDomain(null);
								mWords.setFrequency(0);
								mWords.setSort("");
								wordList.add(mWords);
								if(!DictUtil.match(word)){
									unLogingWordList.add(mWords);
								}
								end = start;
							}
							
						}
						if(end==0){
							isEnd = true;
						}
					}
				}
			}
			Collections.reverse(words);
			Collections.reverse(wordList);
			Collections.reverse(unLogingWordList);
			segResult.setNotLonginWords(unLogingWordList);
			segResult.setWords(wordList);

			
		}
		return segResult;
	}
	
	
	public static void main(String[] args) {
		RMMSeg rm = new RMMSeg();
		System.out.println(rm.seg("礼品卡未激活，根本不能使用，打客服电话客服根本就不清楚情况，而且不止一张卡有这样的问题，好几张卡都不能使用。"));
		long time = System.currentTimeMillis();
		System.out.println(rm.seg("商品质量存在严重问题，废卡也照样发货···可怜出师未捷身先死，悲剧啊·· 打京东电话，推这推那··最后只好打礼品卡专线···结果半天打不通一个，好不容易接通一个··说给我重新激活··满心欢喜··结果，还不是废卡一张，目前还正在争取退钱中··"));
//		System.out.println(rm.seg("买了个iphone4s32块，快递也不让验货就签字，要是少了找谁评理去").size());
		System.out.println(System.currentTimeMillis()-time);
		
//		char a = '&';
//		System.out.println(CharUtils.isAsciiAlphanumeric(a));
	}

}
